package util;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

public class ImageLoader {

	private final Map<String, BufferedImage> images = new HashMap<String, BufferedImage>();
	
	private final String path;
	
	public ImageLoader() {
		path = "/img/";
	}
	
	public ImageLoader(String path) {
		this.path = path;
	}

	public BufferedImage load(String file) {
		if (images.containsKey(file)) {
			return images.get(file);
		}
		try {
			URL url = this.getClass().getResource(this.path + file);
			BufferedImage img = ImageIO.read(url);
			images.put(file, img);
			return img;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (NullPointerException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void clear() {
		images.clear();
	}
}
